package com.zzh.wsss.service.impl;

import com.zzh.wsss.mapper.PunchClockOperateMapper;
import com.zzh.wsss.mapper.UserOperateMapper;
import com.zzh.wsss.pojo.*;
import com.zzh.wsss.service.UserPunchClockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserPunchClockServiceImpl implements UserPunchClockService {

    @Autowired
    private PunchClockOperateMapper punchClockOperateMapper;

    @Autowired
    private UserOperateMapper userOperateMapper;

    public Map<String,Object> punchClock(String username) {
        Map<String,Object> map=new HashMap<String, Object>();
        Integer userId=userOperateMapper.getUserId(username).getId();
        Integer userJobId=userOperateMapper.getUserMag(username).getJobId();
        if(userJobId!=null){
            LocalDateTime now = LocalDateTime.now();
            if(userPunchClockState(userId,userJobId)) {
                punchClockOperateMapper.addPunchClock(userId,userJobId,1,now.toString());
                map.put("result",1);
            }else {
                punchClockOperateMapper.addPunchClock(userId,userJobId,2,now.toString());
                map.put("result",2);
                List<Long> workHours=(List<Long>) countWorkHours(userId,userJobId).get("workHours");
                map.put("workHour",workHours.get(workHours.size()-1));
            }
        }else{
            map.put("result",0);
        }
        return map;
    }

    public Boolean punchClockState(String username) {
        Integer userId=userOperateMapper.getUserId(username).getId();
        Integer userJobId=userOperateMapper.getUserMag(username).getJobId();
        if(userJobId!=null){
            return userPunchClockState(userId,userJobId);
        }else{
            return true;
        }
    }

    public Map<String, Object> getUserPunchClockInfo(String username) {
        int t=0;
        Map<String,Object> map=new HashMap<String, Object>();
        Integer userId=userOperateMapper.getUserId(username).getId();
        Integer userJobId=userOperateMapper.getUserMag(username).getJobId();
        Map<String,Object> m=countWorkHours(userId,userJobId);
        map.put("state",m.get("state"));
        List<UserPunchClockInfo> userPunchClockInfos=new ArrayList<>();
        List<PunchClock> punchClocks= (List<PunchClock>)m.get("punchClocks");
        List<Long> workHours=(List<Long>) m.get("workHours");
        for(int i=0;i<punchClocks.size()-1;i+=2){
            UserPunchClockInfo userPunchClockInfo=new UserPunchClockInfo(
                    t+1,
                    punchClocks.get(i).getJobTime(),
                    punchClocks.get(i+1).getJobTime(),
                    workHours.get(t)
            );
            userPunchClockInfos.add(userPunchClockInfo);
            t++;
        }
        if(Integer.parseInt(m.get("state").toString())!=0){
            UserPunchClockInfo userPunchClockInfo=new UserPunchClockInfo(
                    t+1,
                    punchClocks.get(punchClocks.size()-1).getJobTime(),
                    "",
                    0l
            );
            userPunchClockInfos.add(userPunchClockInfo);
        }
        map.put("totalWorkHour",m.get("totalWorkHour"));
        map.put("userPunchClockInfos",userPunchClockInfos);
        return map;
    }

    //true:上班打卡
    private Boolean userPunchClockState(Integer userId,Integer userJobId) {
        Integer startNum=punchClockOperateMapper.getPunchClockNum(userId,userJobId,1);
        Integer endNum=punchClockOperateMapper.getPunchClockNum(userId,userJobId,2);
        if(startNum!=endNum)return false;
        else return true;
    }

    //计算工作的工作时间
    // 打卡表中的id
    public Map<String,Object> countWorkHours(Integer userId, Integer JobId){
        Map<String,Object> map=new HashMap<String, Object>();
        List<Long> workHours=new ArrayList<Long>();
        Long totalWorkHour=0l;
        List<PunchClock> punchClocks=punchClockOperateMapper.getPunchClocks(userId,JobId);
        map.put("punchClocks",punchClocks);
        if(punchClocks.size()%2==0)map.put("state",0);
        else map.put("state",1);
        for (int i=0;i<punchClocks.size()-1;i+=2){
            LocalDateTime p1 = LocalDateTime.parse(punchClocks.get(i).getJobTime(),
                                                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            LocalDateTime p2 = LocalDateTime.parse(punchClocks.get(i+1).getJobTime(),
                                                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            Duration duration = Duration.between(p1,p2);
            totalWorkHour+=duration.getSeconds();
            workHours.add(duration.getSeconds());
        }
        map.put("totalWorkHour",totalWorkHour);
        map.put("workHours",workHours);//单位为秒 long类型
        return map;
    }

}
